package gui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import client.ClientConsole;

import logic.BookItem;
import logic.Librarian;
import logic.Message;
import logic.Reader;
/** 
 * Creates a return book form
 * return a book for a reader, book inserted
 */
public class ReturnBookForm extends JPanel implements ActionListener{

	/**  GUI Panel used for Design*/
	private JPanel mainPanel,searchPanel,centerPanel,bottomPanel;
	private JTextField catalogNumber, readerId;
	private JTextPane comment;
	private JButton returnBook;
	private JButton back;
	/** confirmation label show feedback form the server*/
	private JLabel confirmationLabel,barcode,readerLabel,commentLable;
	/** saves the properties of the GridBagLayout for*/
	GridBagConstraints gbc = new GridBagConstraints();
	
	/**
	 * Default constructor-Builds the return Book form
	 */
	public ReturnBookForm()
	{
		super();
		setSize(300, 300);
		mainPanel=new JPanel();
		mainPanel.setLayout(new BorderLayout());
		searchPanel=new JPanel(new GridBagLayout());
		barcode=new JLabel("Catalog Number: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=0;
		searchPanel.add(barcode,gbc);
		catalogNumber=new JTextField(20);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=0;
		gbc.insets= new Insets(5,5,5,5);
		searchPanel.add(catalogNumber,gbc);
		readerLabel=new JLabel("Reader id: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=1;
		gbc.insets= new Insets(5,5,5,5);
		searchPanel.add(readerLabel,gbc);
		readerId= new JTextField(20);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=1;
		gbc.insets= new Insets(5,5,5,5);
		searchPanel.add(readerId,gbc);
		
		commentLable=new JLabel("comments: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=2;
		searchPanel.add(commentLable,gbc);
		
		comment=new JTextPane();
		comment.setSize(50, 50);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=2;
		gbc.insets= new Insets(5,5,5,5);
		searchPanel.add(comment,gbc);
		
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=3;
		gbc.gridwidth=2;
		gbc.insets= new Insets(5,5,5,5);

		returnBook= new JButton("Return Book");
		searchPanel.add(returnBook,gbc);
		mainPanel.add(searchPanel,BorderLayout.NORTH);
		centerPanel=new JPanel();
		confirmationLabel=new JLabel();
		centerPanel.add(confirmationLabel);
		mainPanel.add(centerPanel,BorderLayout.CENTER);
		bottomPanel=new JPanel();
		back=new JButton("Back");
		bottomPanel.add(back);
		mainPanel.add(bottomPanel,BorderLayout.SOUTH);
		add(mainPanel);
		returnBook.addActionListener(this);
		back.addActionListener(this);
		
	}
	//Handles user's clicks on buttons
	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource()==back)
		{
			this.setVisible(false);
			if (ClientConsole.reader instanceof LibraryManager)
				ClientConsole.aFrame.setContentPane(new LibraryManagerWindow());
			else if (ClientConsole.reader instanceof Librarian)
				ClientConsole.aFrame.setContentPane(new LibrarianWindow());
		}
		else if (ae.getSource()==returnBook)
		{
			if (readerId.getText().isEmpty()||catalogNumber.getText().isEmpty())
			{
				confirmationLabel.setText("Please fill all fields");
				confirmationLabel.setVisible(true);
			}
			else	//determine what extend button was pressed and send a request to server
			{
				Message m;
				Object[] o;
				o=new Object[3];
				Reader r=new Reader(Integer.parseInt(readerId.getText()));
				
				BookItem item=new BookItem(Integer.parseInt(catalogNumber.getText()));
				o[0]=(Object)item;
				o[1]=(Object)r;
				o[2]=comment.getText();
								
				m=new Message("Return Book",o);
				ClientConsole.HandleMessageFromGui(m);
				
				m=(Message)ClientConsole.client.getMessage();
				
				confirmationLabel.setText(m.getCommand());
				confirmationLabel.setVisible(true);
			}
		}
	}
}
